<?php
/**
 * Created by PhpStorm.
 * User: qzy008
 * Date: 2018/9/21
 * Time: 16:04
 */

namespace app\api\controller\v1;

use app\api\controller\BaseController;
use app\api\model\Category as CategoryModel;
use app\api\validate\category\CategoryCreateOrEdit;
use app\common\validate\IDMustBePositiveInt;
use app\lib\exception\MissException;

class Category extends BaseController
{
    public function allCategory()
    {
        $result = CategoryModel::all();
        if (!$result) {
            throw new MissException(['msg' => '没有找到文章分类']);
        }
        return $this->returnHandle($result);
    }

    public function createCategory()
    {
        $validate = new CategoryCreateOrEdit();
        $validate->goCheck(['scene' => 'create']);
        $params = $validate->getParameters();
        $result = CategoryModel::create($params);
        return $this->returnHandle($result);
    }

    public function deleteCategory($id)
    {
        (new IDMustBePositiveInt())->goCheck();
        $result = CategoryModel::destroy($id);
        return $this->returnHandle($result);
    }

    public function updateCategory()
    {
        $validate = new CategoryCreateOrEdit();
        $validate->goCheck();
        $params = $validate->getParameters();
        $result = CategoryModel::update($params);
        return $this->returnHandle($result);
    }
}